﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using DotNet.Configuration;
using DotNet.Mvc;
using XNWebAPI.Models;
using XNWebService;

namespace XNWebAPI.Controllers
{
    public class TrainController : JsonController
    {
        private List<StationModel> Stations;

        /// <summary>
        /// 火车时刻查询
        /// </summary>
        /// <param name="from">出发站（如：上海虹桥）</param>
        /// <param name="to">到达站（如：温州南）</param>
        /// <param name="date">出发日期，格式（yyyy-MM-dd，如：2016-05-01）</param>
        public async Task<ActionResult> TrainSearch(string from, string to, string date)
        {
            var result = await TrainService.TrainSearch(from, to, date);
            return new JsonStringResult(result);
        }

        /// <summary>
        /// 火车站点查询
        /// </summary>
        public ActionResult TrainStations()
        {
            var result = System.IO.File.ReadAllText(Server.MapPath("~/TrainStations.json"));
            return new JsonStringResult(result);
        }

        /// <summary>
        /// 火车站点查询
        /// </summary>
        public ActionResult TrainStationsSearch(string term)
        {
            if (Stations == null)
            {
                Stations = JsonHelper.Deserialize(Server.MapPath("~/TrainStations.json"), new List<StationModel>());
            }
            //foreach (var item in Stations)
            //{
            //    item.spell = SpellHelper.ConvertSpell(item.name);
            //    //item.spell = SpellHelper.ConvertSpell(item.name) + "/" + SpellHelper.ConvertFullSpell(item.name);
            //}
            //JsonHelper.Serialize(Server.MapPath("~/TrainStations.json"), Stations);
            var list = Stations.Where(p => p.name.StartsWith(term, StringComparison.OrdinalIgnoreCase)
            || p.spell.StartsWith(term, StringComparison.OrdinalIgnoreCase));
            return Json(list);
        }
    }
}